﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Quartz.Net.Model;
using System.Data.OracleClient;
using System.Data;

namespace Quartz.Net.DAL
{
    /// <summary>
    /// 任务运行表
    /// </summary>
    public class Task_Quartz_RunDAL
    {
        
        public int InsertRun(Task_Quartz_RunModel model)
        {
            string strSql = @"insert into TASK_QUARTZ_RUN (NGUID,JOBGUID,LASTTIME,NEXTTIME,VALID)
                            values (:NGUID,:JOBGUID,:LASTTIME,:NEXTTIME,:VALID)";

            OracleParameter[] para = {
                                         new OracleParameter(":NGUID",OracleType.VarChar,200),
                                         new OracleParameter(":JOBGUID",OracleType.VarChar,200),
                                         new OracleParameter(":LASTTIME",OracleType.DateTime),
                                         new OracleParameter(":NEXTTIME",OracleType.DateTime),
                                         new OracleParameter(":VALID",OracleType.Char,1)
                                     };

            para[0].Value = model.NGUID;
            para[1].Value = model.JOBGUID;
            para[2].Value = model.LASTTIME;
            para[3].Value = model.NEXTTIME;
            para[4].Value = model.VALID;
            return DalBase.ExecuteNonQuery(strSql, para);

        }

        public int DeleteRun(string Jobnguid)
        {
            string strSql = @"delete from TASK_QUARTZ_RUN where JOBGUID=:JOBGUID";
            OracleParameter[] para = {
                                         new OracleParameter(":JOBGUID",OracleType.VarChar,200)
                                     };

            para[0].Value = Jobnguid;
            return DalBase.ExecuteNonQuery(strSql, para);
        }

        public int DeleteRun()
        {
            string strSql = @"delete from TASK_QUARTZ_RUN";
           
            return DalBase.ExecuteNonQuery(strSql, null);
        }
        public int UpdateRun(Task_Quartz_RunModel model)
        {
            string strSql = @"update TASK_QUARTZ_RUN set  LASTTIME=:LASTTIME ,NEXTTIME=:NEXTTIME,VALID=:VALID where NGUID=:NGUID";
            OracleParameter[] para = {
                                         new OracleParameter(":LASTTIME",OracleType.DateTime),
                                         new OracleParameter(":NEXTTIME",OracleType.DateTime),
                                         new OracleParameter(":VALID",OracleType.Char,1),
                                         new OracleParameter(":NGUID",OracleType.VarChar,200)
                                     };

            para[0].Value = model.LASTTIME;
            para[1].Value = model.NEXTTIME;
            para[2].Value = model.VALID;
            para[3].Value = model.NGUID;
            return DalBase.ExecuteNonQuery(strSql, para);

        }

        public IEnumerable<Task_Quartz_RunModel> GetOntInfo(string jobguid)
        {
            List<Task_Quartz_RunModel> Task_Quartz_RunModel = new List<Task_Quartz_RunModel>();

            string strSql = "select NGUID,JOBGUID,LASTTIME,NEXTTIME,VALID from task_quartz_run where JOBGUID=:nguid and valid = '1'";
            OracleParameter[] para = {
                                         new OracleParameter(":nguid",OracleType.VarChar,200)
                                     };
            para[0].Value = jobguid;
            DataTable dt = DalBase.ExecuteDataTable(strSql, para);
            foreach (DataRow dr in dt.Rows)
            {
                Task_Quartz_RunModel.Add(ToModel(dr));
            }


            return Task_Quartz_RunModel;
        }
        public IEnumerable<Task_Quartz_RunModel> GetInfo()
        {
            List<Task_Quartz_RunModel> Task_Quartz_RunModel = new List<Task_Quartz_RunModel>();

            string strSql = "select NGUID,JOBGUID,LASTTIME,NEXTTIME,VALID from task_quartz_run where valid = '1'";
           
            DataTable dt = DalBase.ExecuteDataTable(strSql, null);
            foreach (DataRow dr in dt.Rows)
            {
                Task_Quartz_RunModel.Add(ToModel(dr));
            }


            return Task_Quartz_RunModel;
        }

        private Task_Quartz_RunModel ToModel(DataRow dr)
       {
           Task_Quartz_RunModel model = new Task_Quartz_RunModel();
           model.NGUID = Convert.ToString(dr["NGUID"]);
           model.JOBGUID = Convert.ToString(dr["JOBGUID"]);
           model.LASTTIME = Convert.ToDateTime(dr["LASTTIME"]);
           model.NEXTTIME = Convert.ToDateTime(dr["NEXTTIME"]);
           model.VALID = Convert.ToString(dr["VALID"]);
           return model;
       }
    }
}
